package com.tmt.datatable.editor.ext.fileinfo;

import com.tmt.annotation.SyncLoadFile;
import com.tmt.datatable.model.FileInfo;
import com.tmt.datatable.model.FileWrapper;
import com.tmt.helper.ReflectHelper;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * Created by ThreeManTeam on 2017/6/7.
 */
public class DefaultFileInfoLoader {

    public static <T> FileWrapper getFileWrapper(List<T> objList, Class idClazz,  boolean enableRecursion, IFileInfoFinder fileInfoFinder){
        FileWrapper fileWrapper = new FileWrapper(new HashMap<>());
        objList.stream().forEach(x->{
            List<FileInfo> list = getFileInfo(x, idClazz, null, true, fileInfoFinder);
            list.stream().forEach(y->{
                fileWrapper.getFiles().put(y.getId(), y);
            });
        });
        return fileWrapper;
    }

    /**
     * 获取对象绑定的所有图片文件。
     * @param obj
     * @param idClazz 实体ID的注释类。
     * @param fileId 文件实体ID。
     * @param enableRecursion 是否递归加载文件
     * @param <T>
     * @return
     */
    public static <T> List<FileInfo> getFileInfo(T obj, Class idClazz, String fileId,
                                                  boolean enableRecursion, IFileInfoFinder fileInfoFinder){
        List<FileInfo> list = new ArrayList<>();

        List<Field> fileFieldList = fileInfoFinder.getFileFieldList(obj);
        fileFieldList.stream().forEach(x->{
            try {
                FileInfo fi = fileInfoFinder.getFileInfo(obj, x, fileId);
                list.add(fi);
            }catch (Exception ex){}
        });

        if (!enableRecursion)
            return list;

        //加载属性类绑定的文件。
        loadPropFile(obj, idClazz, fileInfoFinder, list);
        return list;
    }

    /**
     * 加载SyncLoadFile注释标记的属性类上绑定的文件。
     * @param obj
     * @param idClazz 实体ID字段的上的Id标识注释类。
     * @param fileInfoFinder
     * @param fileInfoList
     * @param <T>
     */
    private static <T> void loadPropFile(T obj, Class idClazz, IFileInfoFinder fileInfoFinder, List<FileInfo> fileInfoList){
        List<Field> fieldList = ReflectHelper.findAllFieldByAnnotation(obj.getClass(), SyncLoadFile.class, -1);
        fieldList.stream().forEach(x->{
            try {
                SyncLoadFile annotation = x.getAnnotation(SyncLoadFile.class);
                Object value = ReflectHelper.getFieldValue(obj, x.getName());
                if (value != null) {
                    if (value instanceof Collection) {
                        ((Collection) value).stream().forEach(y -> {
                            try {
                                String id = ReflectHelper.getFieldValueByAnnotation(y, idClazz, "0").toString();
                                List<FileInfo> tmpList = getFileInfo(y, idClazz, id, annotation.enableRecursion(), fileInfoFinder);
                                fileInfoList.addAll(tmpList);
                            } catch (Exception ex) {
                            }
                        });
                    } else {
                        String id = ReflectHelper.getFieldValueByAnnotation(value, idClazz, "0").toString();
                        List<FileInfo> tmpList = getFileInfo(value, idClazz, id, annotation.enableRecursion(), fileInfoFinder);
                        fileInfoList.addAll(tmpList);
                    }
                }
            }catch (Exception ex){}
        });
    }

}
